package com.linghang.back.controller;

import com.linghang.back.dto.ArticleDTO;
import com.linghang.back.dto.PageResult;
import com.linghang.back.dto.ResponseResult;
import com.linghang.back.service.ArticleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 文章API控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/articles")
@CrossOrigin // 允许跨域请求
@Tag(name = "文章管理", description = "文章的增删改查操作")
public class ArticleController {

    @Autowired
    private ArticleService articleService;
    
    /**
     * 获取文章列表
     * @param pageNum 页码（从1开始）
     * @param pageSize 每页数量
     * @param type 文章类型（可选）
     * @return 分页文章列表
     */
    @Operation(
        summary = "获取文章列表",
        description = "分页获取文章列表，可根据文章类型筛选"
    )
    @GetMapping
    public ResponseResult<PageResult<ArticleDTO>> getArticleList(
            @Parameter(description = "页码，从1开始") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") Integer pageSize,
            @Parameter(description = "文章类型，可选") @RequestParam(required = false) String type) {
        try {
            PageResult<ArticleDTO> result = articleService.getArticleList(pageNum, pageSize, type);
            return ResponseResult.success(result);
        } catch (Exception e) {
            log.error("获取文章列表异常", e);
            return ResponseResult.fail(e.getMessage());
        }
    }
    
    /**
     * 获取文章详情
     * @param id 文章ID
     * @return 文章详情
     */
    @Operation(
        summary = "获取文章详情", 
        description = "根据文章ID获取文章详细信息，同时增加阅读量"
    )
    @ApiResponse(responseCode = "200", description = "成功获取文章详情")
    @ApiResponse(responseCode = "404", description = "文章不存在")
    @ApiResponse(responseCode = "500", description = "服务器内部错误")
    @GetMapping("/{id}")
    public ResponseResult<ArticleDTO> getArticleDetail(@Parameter(description = "文章ID") @PathVariable Long id) {
        try {
            // 增加阅读量
            articleService.incrementReadCount(id);
            
            ArticleDTO article = articleService.getArticleDetail(id);
            return ResponseResult.success(article);
        } catch (Exception e) {
            log.error("获取文章详情异常", e);
            return ResponseResult.fail(e.getMessage());
        }
    }
    
    /**
     * 搜索文章
     * @param keyword 关键词
     * @param pageNum 页码
     * @param pageSize 每页数量
     * @return 搜索结果
     */
    @Operation(
        summary = "搜索文章", 
        description = "根据关键词搜索文章"
    )
    @ApiResponse(responseCode = "200", description = "成功搜索文章")
    @ApiResponse(responseCode = "500", description = "服务器内部错误")
    @GetMapping("/search")
    public ResponseResult<PageResult<ArticleDTO>> searchArticles(
            @Parameter(description = "搜索关键词") @RequestParam String keyword,
            @Parameter(description = "页码，从1开始") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") Integer pageSize) {
        try {
            PageResult<ArticleDTO> result = articleService.searchArticles(keyword, pageNum, pageSize);
            return ResponseResult.success(result);
        } catch (Exception e) {
            log.error("搜索文章异常", e);
            return ResponseResult.fail(e.getMessage());
        }
    }
    
    /**
     * 手动触发同步微信公众号文章
     * 实际生产环境中，此接口应该添加适当的权限控制
     */
    @Operation(
        summary = "同步微信公众号文章", 
        description = "手动触发同步微信公众号文章到系统中"
    )
    @ApiResponse(responseCode = "200", description = "同步成功")
    @ApiResponse(responseCode = "500", description = "同步失败")
    @PostMapping("/sync")
    public ResponseResult<Void> syncWechatArticles() {
        try {
            articleService.syncWechatArticles();
            return ResponseResult.success("同步成功", null);
        } catch (Exception e) {
            log.error("手动同步文章异常: {}", e.getMessage(), e);
            return ResponseResult.fail(e.getMessage());
        }
    }
} 